package com.task.scheduling.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @program: task-scheduling
 * @description: 分发器的状态
 * @author: zhouliangliang
 * @create: 2019-03-11 16:26
 **/
public class DispatcherHolder {

    private static Logger log = LoggerFactory.getLogger(DispatcherHolder.class);
    private static Lock lock = new ReentrantLock();

    private static boolean dispatcher = true;

    private DispatcherHolder() {

    }

    public static Boolean getDispatcherStatus() {
        return dispatcher;
    }

    public static void stopDispatcher() {
        try {
            lock.lock();
            dispatcher = false;
        } catch (Exception e) {
            log.error("stop dispatcher error!" + e.getMessage(), e);
        } finally {
            lock.unlock();
        }
    }

    public static void startDispatcher() {
        try {
            lock.lock();
            dispatcher = true;
        } catch (Exception e) {
            log.error("start dispatcher error!" + e.getMessage(), e);
        } finally {
            lock.unlock();
        }
    }

}
